﻿using System;
using System.Collections.Generic;

using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System.Threading;
using Microsoft.Xna.Framework.Content;
using RebEngine.Interfaces;
using RebEngine.Sound;
using Microsoft.Xna.Framework.Media;


namespace RebEngine.Managers
{
    public class SoundManager : GameComponent
    {
        private static Dictionary<string, IRebSound> _sounds = new Dictionary<string, IRebSound>();
        private static bool _initialized = false;
        private static int _soundsLoaded = 0;
        
        

        public static bool Initialized
        {
            get { return _initialized; }
        }

        public SoundManager(Game game)
            : base(game)
        {
            AddSound("NoSound");
        }

        public static void AddSound(string soundName)
        {
            RebSound newSound = new RebSound(soundName);
            if (soundName != null && !_sounds.ContainsKey(soundName))
            {
                
                _sounds.Add(soundName, newSound);
                if (_initialized)
                {
                        newSound.LoadContent();
                }
            LogManager.Write("Sound Loaded: " + soundName);    
            }
        }

        public static void RemoveSound(string soundName)
        {
            if (soundName != null && _sounds.ContainsKey(soundName))
            {
                if (_initialized)
                {
                    _sounds[soundName].UnloadContent();
                    _sounds.Remove(soundName);
                    _soundsLoaded--;
                    LogManager.Write("Sound Deleted: " + soundName);
                }
            }
        }

        public static IRebSound GetSound(string soundName)
        {
            if (soundName != null && _sounds.ContainsKey(soundName) && _sounds[soundName].BaseSound != null)
            {
                return _sounds[soundName];
            }
            else
            {
                return _sounds["NoSound"];
            }
        }

        public static void PlaySound(string soundName)
        {
            if (_sounds[soundName].SoundInstance == null )
            {
                _sounds[soundName].SoundInstance = _sounds[soundName].BaseSound.CreateInstance();
                _sounds[soundName].SoundInstance.Play();
                return;
            }

            if (_sounds[soundName].SoundInstance.State == SoundState.Playing)
            {
                return;
            }

            if (_sounds[soundName].SoundInstance.State == SoundState.Stopped)
            {
                _sounds[soundName].SoundInstance.Dispose();
                _sounds[soundName].SoundInstance = null;
                return;
            }
        }

        public override void Initialize()
        {
            base.Initialize();

            foreach (IRebSound sound in _sounds.Values)
            {
                if (!sound.ReadyToPlay)
                {
                    sound.LoadContent();
                    _soundsLoaded++;
                }
            }
            _initialized = true;
        }

    }
}
